<?php

class SiteController extends Controller
{
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
			'contact'=>'application.controllers.site.ContactAction',
		);
	}
	
	public function filters()
	{
		return array(
			'AccessPage + page',
			array(
				'application.filters.ContactFilter + contact'
			),
		);
	}
	
	public function filterAccessPage($pageAction)
	{
		$pageAction->run();
	}
	
	public function onClick($event)
	{
		echo "event 1";
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		/*$comp = new TestComponent();
		$comp->onAbc = array($this, 'onClick');
		$comp->onAbc = array(new Test2, 'onClick');
		$comp->onAbc = function($event) {
			echo "event 2";
		};
		
		$this->attachBehavior('asd', $comp);
		$this->abc();
		//CVarDumper::dump($comp);exit;
		exit;*/
        // CVarDumper::dump($this);exit;
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		$this->render('index');
	}
	
	public function actionTestvalidator()
	{
		$model=new TestValidator;

		// uncomment the following code to enable ajax-based validation
		/*
		if(isset($_POST['ajax']) && $_POST['ajax']==='test-validator-testvalidator-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
		*/

		if(isset($_POST['TestValidator']))
		{
			$model->attributes=$_POST['TestValidator'];
			if($model->validate())
			{
				// form inputs are valid, do something here
				return;
			}
		}
		$this->render('testvalidator',array('model'=>$model));
	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
	    if($error=Yii::app()->errorHandler->error)
	    {
	    	if(Yii::app()->request->isAjaxRequest)
	    		echo $error['message'];
	    	else
	        	$this->render('error', $error);
	    }
	}
}

class TestComponent extends CBehavior
{
	private $_abc;
	
	public function setAbc($value)
	{
		$this->_abc = $value;
	}
	
	public function onAbc($event)
	{
		$this->raiseEvent('onAbc', $event);
	}
	
	public function abc()
	{
		echo "ABC";
		$this->onAbc(new CEvent);
	}
}

class Test2 extends CBehavior
{
	public function onClick()
	{
		echo 'This is Test 2';
	}
}

class TestClass
{
}